#!/bin/sh
#
# Installer for support files needed by Pacemaker's Cluster Test Suite
#
# Copyright 2018 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# or later (GPLv2+) WITHOUT ANY WARRANTY.
#

USAGE_TEXT="Usage: $0 <install|uninstall|--help>"

HELP_TEXT="$USAGE_TEXT
Commands (must be run as root):
 install      Install support files needed by Pacemaker CTS
 uninstall    Remove support files needed by Pacemaker CTS"

# These constants must track crm_exit_t values
CRM_EX_OK=0
CRM_EX_ERROR=1
CRM_EX_USAGE=64

UNIT_DIR="@systemdunitdir@"
LIBEXEC_DIR="@libexecdir@/pacemaker"
INIT_DIR="@INITDIR@"
SBIN_DIR="@sbindir@"
DATA_DIR="@datadir@/pacemaker/tests/cts"
UPSTART_DIR="/etc/init"

DUMMY_DAEMON="pacemaker-cts-dummyd"
DUMMY_DAEMON_UNIT="pacemaker-cts-dummyd@.service" 

LSB_DUMMY="LSBDummy"
UPSTART_DUMMY="pacemaker-cts-dummyd.conf"
FENCE_DUMMY="fence_dummy"
FENCE_DUMMY_ALIASES="fence_dummy_auto_unfence fence_dummy_no_reboot"

# If the install directory doesn't exist, assume we're in a build directory.
if [ ! -d "$DATA_DIR" ]; then
    # If readlink supports -e (i.e. GNU), use it.
    readlink -e / >/dev/null 2>/dev/null
    if [ $? -eq 0 ]; then
        DATA_DIR="$(dirname "$(readlink -e "$0")")"
    else
        DATA_DIR="$(dirname "$0")"
    fi
fi

usage() {
    echo "Error:" "$@"
    echo "$USAGE_TEXT"
    exit $CRM_EX_USAGE
}

must_be_root() {
    if ! [ "$(id -u)" = "0" ]; then
        usage "this command must be run as root"
        return $CRM_EX_ERROR
    fi
    return $CRM_EX_OK
}

support_uninstall() {
    must_be_root || return $CRM_EX_ERROR

    if [ -e "$UNIT_DIR/$DUMMY_DAEMON_UNIT" ]; then
        echo "Removing $UNIT_DIR/$DUMMY_DAEMON_UNIT ..."
        rm -f "$UNIT_DIR/$DUMMY_DAEMON_UNIT"
        systemctl daemon-reload # Ignore failure
    fi

    for FILE in \
        "$LIBEXEC_DIR/$DUMMY_DAEMON" \
        "$UPSTART_DIR/$UPSTART_DUMMY" \
        "$SBIN_DIR/$FENCE_DUMMY" \
        "$INIT_DIR/$LSB_DUMMY"
    do
        if [ -e "$FILE" ]; then
            echo "Removing $FILE ..."
            rm -f "$FILE"
        fi
    done
    for ALIAS in $FENCE_DUMMY_ALIASES; do \
        FILE="$SBIN_DIR/$ALIAS" 
        if [ -L "$FILE" ] || [ -e "$FILE" ]; then
            echo "Removing $FILE ..."
            rm -f "$FILE"
        fi
    done

    return $CRM_EX_OK
}

support_install() {
    support_uninstall || return $CRM_EX_ERROR
    cd "$DATA_DIR" || return $CRM_EX_ERROR
    if [ -d "$UNIT_DIR" ]; then

        echo "Installing $DUMMY_DAEMON ..."
        mkdir -p "$LIBEXEC_DIR"
        install -m 0755 "$DUMMY_DAEMON" "$LIBEXEC_DIR" || return $CRM_EX_ERROR

        echo "Installing $DUMMY_DAEMON_UNIT ..."
        install -m 0644 "$DUMMY_DAEMON_UNIT" "$UNIT_DIR" || return $CRM_EX_ERROR
        systemctl daemon-reload # Ignore failure
    fi

    echo "Installing $FENCE_DUMMY to $SBIN_DIR ..."
    mkdir -p "$SBIN_DIR"
    install -m 0755 "$FENCE_DUMMY" "$SBIN_DIR" || return $CRM_EX_ERROR
    for alias in $FENCE_DUMMY_ALIASES; do \
        echo "Installing $alias to $SBIN_DIR ..."
        ln -s "$FENCE_DUMMY" "$SBIN_DIR/$alias" || return $CRM_EX_ERROR
    done

    echo "Installing $LSB_DUMMY to $INIT_DIR ..."
    mkdir -p "$INIT_DIR"
    install -m 0755 "$LSB_DUMMY" "$INIT_DIR" || return $CRM_EX_ERROR

    if [ -d "$UPSTART_DIR" ] && [ -f "$UPSTART_DUMMY" ]; then
        echo "Installing $UPSTART_DUMMY to $UPSTART_DIR ..."
        install -m 0644 "$UPSTART_DUMMY" "$UPSTART_DIR" || return $CRM_EX_ERROR
    fi
    return $CRM_EX_OK
}

COMMAND=""
while [ $# -gt 0 ] ; do
    case "$1" in
        --help)
            echo "$HELP_TEXT"
            exit $CRM_EX_OK
            ;;
        install|uninstall)
            COMMAND="$1"
            shift
            ;;
        *)
            usage "unknown option '$1'"
            ;;
    esac
done
case "$COMMAND" in
    install)   support_install              ;;
    uninstall) support_uninstall            ;;
    *)         usage "must specify command" ;;
esac
